package com.wbs.shop.nuby.view.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.wbs.shop.nuby.R;

/**
 * Created by zhoucl on 2018/5/21 0021.
 */
public class AlertDialog extends Dialog implements View.OnClickListener {
    private OnClickListener mPositiveButtonListener;
    private OnClickListener mNegativeButtonListener;

    public AlertDialog(Builder builder) {
        super(builder.mContext, R.style.loadingdialog);
        setContentView(R.layout.dialog_alert);
        mPositiveButtonListener = builder.mPositiveButtonListener;
        mNegativeButtonListener = builder.mNegativeButtonListener;
        ((TextView) findViewById(R.id.message)).setText(builder.mMessage);
        TextView positiveButton = (TextView) findViewById(R.id.positive_button);
        positiveButton.setText(builder.mPositiveButtonText);
        positiveButton.setOnClickListener(this);
        TextView negativeButton = (TextView) findViewById(R.id.negative_button);
        if (TextUtils.isEmpty(builder.mNegativeButtonText))
            negativeButton.setVisibility(View.GONE);
        negativeButton.setText(builder.mNegativeButtonText);
        negativeButton.setOnClickListener(this);
        setCancelable(builder.mCancelable);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = (int) (builder.mContext.getResources().getDisplayMetrics().widthPixels * 0.65);
        getWindow().setAttributes(attributes);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.positive_button:
                if (mPositiveButtonListener != null)
                    mPositiveButtonListener.onClick(this, 0);
                break;
            case R.id.negative_button:
                if (mNegativeButtonListener != null)
                    mNegativeButtonListener.onClick(this, 1);
                break;
        }
    }

    public static class Builder {
        Context mContext;
        String mTitle;
        String mMessage;
        String mPositiveButtonText;
        OnClickListener mPositiveButtonListener;
        String mNegativeButtonText;
        OnClickListener mNegativeButtonListener;
        boolean mCancelable = true;

        public Builder(@NonNull Context context) {
            mContext = context;
        }

        public Builder setTitle(@StringRes int titleId) {
            mTitle = mContext.getString(titleId);
            return this;
        }

        public Builder setTitle(@Nullable String title) {
            mTitle = title;
            return this;
        }

        public Builder setMessage(@StringRes int messageId) {
            mMessage = mContext.getString(messageId);
            return this;
        }

        public Builder setMessage(@Nullable String message) {
            mMessage = message;
            return this;
        }

        public Builder setPositiveButton(@StringRes int textId, OnClickListener listener) {
            mPositiveButtonText = mContext.getString(textId);
            mPositiveButtonListener = listener;
            return this;
        }

        public Builder setPositiveButton(String text, OnClickListener listener) {
            mPositiveButtonText = text;
            mPositiveButtonListener = listener;
            return this;
        }

        public Builder setNegativeButton(@StringRes int textId, OnClickListener listener) {
            mNegativeButtonText = mContext.getString(textId);
            mNegativeButtonListener = listener;
            return this;
        }

        public Builder setNegativeButton(String text, final OnClickListener listener) {
            mNegativeButtonText = text;
            mNegativeButtonListener = listener;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mCancelable = cancelable;
            return this;
        }

        public AlertDialog create() {
            return new AlertDialog(this);
        }
    }
}
